package com.ltkj.shop.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ltkj.common.core.utils.DateUtils;
import com.ltkj.common.core.utils.StringUtils;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.datascope.annotation.DataScope;
import com.ltkj.common.security.utils.SecurityUtils;
import com.ltkj.shop.domain.ShopRestaurantMenu;
import com.ltkj.shop.domain.vo.ShopRestMenuVo;
import com.ltkj.shop.domain.vo.ShopRestaurantVo;
import com.ltkj.shop.mapper.ShopRestaurantBizMapper;
import com.ltkj.shop.mapper.ShopRestaurantMenuBizMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ltkj.shop.mapper.ShopRestaurantMapper;
import com.ltkj.shop.domain.ShopRestaurant;
import com.ltkj.shop.service.IShopRestaurantService;

/**
 * 餐饮商户Service业务层处理
 * 
 * @author xupf
 * @date 2023-08-29
 */
@Service
public class ShopRestaurantServiceImpl implements IShopRestaurantService 
{
    @Autowired
    private ShopRestaurantMapper shopRestaurantMapper;
    @Autowired
    private ShopRestaurantBizMapper shopRestaurantBizMapper;
    @Autowired
    private ShopRestaurantMenuBizMapper shopRestaurantMenuBizMapper;

    /**
     * 查询餐饮商户
     * 
     * @param id 餐饮商户主键
     * @return 餐饮商户
     */
    @Override
    public ShopRestaurantVo selectShopRestaurantById(Long id)
    {
        return shopRestaurantBizMapper.selectShopRestaurantById(id);
    }

    /**
     * 查询餐饮商户列表
     * 
     * @param shopRestaurant 餐饮商户
     * @return 餐饮商户
     */
    @Override
    @DataScope(storeAlias = "r")
    public List<ShopRestaurantVo> selectShopRestaurantList(ShopRestaurant shopRestaurant)
    {
        return shopRestaurantBizMapper.selectShopRestaurantList(shopRestaurant);
    }

    /**
     * 移动端查询餐饮商户列表
     *
     * @param shopRestaurant 餐饮商户
     * @return 餐饮商户
     */
    @Override
    public List<ShopRestaurantVo> selectList(ShopRestaurant shopRestaurant)
    {
        // 查询营业中餐饮
        shopRestaurant.setStatus("1");
        List<ShopRestaurantVo> shopRestaurantVos = shopRestaurantBizMapper.selectList(shopRestaurant);
        if (StringUtils.isNull(shopRestaurant.getParams().get("limit"))) {
            for (ShopRestaurantVo shopRestaurantVo : shopRestaurantVos) {
                ShopRestaurantMenu shopRestaurantMenu = new ShopRestaurantMenu();
                shopRestaurantMenu.setRestaurantId(shopRestaurantVo.getId());
                List<ShopRestaurantMenu> shopRestaurantMenus = shopRestaurantMenuBizMapper.selectList(shopRestaurantMenu);
                shopRestaurantVo.setRestMenuVos(shopRestaurantMenus.stream().map(ShopRestMenuVo::fromDO).collect(Collectors.toList()));
            }
        }
        return shopRestaurantVos;
    }

    /**
     * 新增餐饮商户
     * 
     * @param shopRestaurant 餐饮商户
     * @return 结果
     */
    @Override
    public AjaxResult insertShopRestaurant(ShopRestaurant shopRestaurant) {
        shopRestaurant.setCreateTime(DateUtils.getNowDate());
        shopRestaurant.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        shopRestaurant.setUserId(SecurityUtils.getLoginUser().getUserid());
        shopRestaurant.setIsDel("0");
        int i;
        try {
            i = shopRestaurantMapper.insertShopRestaurant(shopRestaurant);
        } catch (Exception e) {
            return AjaxResult.error();
        }
        if (i > 0) {
            return AjaxResult.success(shopRestaurant);
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 修改餐饮商户
     * 
     * @param shopRestaurant 餐饮商户
     * @return 结果
     */
    @Override
    public int updateShopRestaurant(ShopRestaurant shopRestaurant)
    {
        shopRestaurant.setUpdateTime(DateUtils.getNowDate());
        shopRestaurant.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return shopRestaurantMapper.updateShopRestaurant(shopRestaurant);
    }

    /**
     * 批量删除餐饮商户
     * 
     * @param ids 需要删除的餐饮商户主键
     * @return 结果
     */
    @Override
    public int deleteShopRestaurantByIds(Long[] ids)
    {
        return shopRestaurantMapper.deleteShopRestaurantByIds(ids);
    }

    /**
     * 删除餐饮商户信息
     * 
     * @param id 餐饮商户主键
     * @return 结果
     */
    @Override
    public int deleteShopRestaurantById(Long id)
    {
        return shopRestaurantMapper.deleteShopRestaurantById(id);
    }
}
